package com.example.roomwordsample.ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.absoluteOffset
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import com.example.roomwordsample.model.Word

@Composable
fun HomeScreen(
    words: List<Word>,
    onAddItemChange: (String) -> Unit,
    onDeleteChange: (Int) -> Unit
) {
    var inputText by remember {
        mutableStateOf("")
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(4.dp)
    ) {
        Column {
            Row(modifier = Modifier.fillMaxWidth()) {
                TextField(
                    value = inputText,
                    onValueChange = { inputText = it },
                    modifier = Modifier
                        .weight(4f)
                        .padding(end = 10.dp)
                )
                Button(
                    onClick = {
                        if (inputText.isNotBlank()) {
                            onAddItemChange(inputText)
                            inputText = ""
                        }
                    },
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .padding(end = 10.dp)
                ) {
                    Text(text = "添加")
                }
            }

            LazyColumn(modifier = Modifier.padding(top = 5.dp)) {
                items(words) { word ->
                    Card(
                        modifier = Modifier
                            .height(40.dp)
                            .fillMaxWidth()
                            .padding(1.dp)
                            .clickable { },
                        elevation = 4.dp,
                    ) {
                        Row {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth(0.9f)
                                    .align(Alignment.CenterVertically),
                                horizontalArrangement = Arrangement.Start
                            ) {
                                Text(text = (words.indexOf(word) + 1).toString())
                                Spacer(modifier = Modifier.padding(end = 10.dp))
                                Text(text = word.name)
                            }

                            Icon(
                                imageVector = Icons.Filled.Delete,
                                contentDescription = "",
                                modifier = Modifier
                                    .align(Alignment.CenterVertically)
                                    .clickable {
                                        onDeleteChange(word.id)
                                    }
                            )
                        }
                    }
                }
            }
        }
    }
}


@Preview
@Composable
fun HomeScreenPreview() {
    val words = listOf(Word(1, "Hello"), Word(2, "王大锤"), Word(3, "李大帅"))
//    HomeScreen(words = words, {})
}